﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;
using vDisk.WindowsPhone;

namespace vDiskClient
{
    public partial class MainPage : PhoneApplicationPage
    {
        private string fileName = "log.txt";
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, RoutedEventArgs e)
        {
            NetService net = new NetService { };
            net.ServicecallBack = LoginCallback;
            Constants.AppKey = "<Your_AppKey>";
            Constants.AppSecret = "<Your_AppSecret>";
            net.Login("<your_username>", "<your_password>");
        }

        private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            if (tbInput.Text.Trim()=="")
            {
                MessageBox.Show("Please write something");
                return;
            }

            //generate file
            GenerateSampleFile(tbInput.Text);

            //start uploading
            NetService net = new NetService();
            net.ServicecallBack = NetCallback;
            UploadFileReq upload = new UploadFileReq
            {
                FileName = fileName,
                SourceFolder = "\\" + fileName,
                DirId = "0",//0 stands for root dir
            };
            net.Upload(upload);
        }

        private void GenerateSampleFile(string text)
        {
            using (var file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = file.OpenFile(fileName, System.IO.FileMode.OpenOrCreate))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        writer.Write(text);
                    }
                }
            }
        }

        private void NetCallback(bool isSuccess, object response, string errormsg)
        {
            if (isSuccess)
            {
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    MessageBox.Show("upload file succeed");
                });
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    MessageBox.Show(errormsg);
                });
            }
        }

        private void LoginCallback(bool isSuccess, object response, string errormsg)
        {
            if (isSuccess)
            {
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    MessageBox.Show("login succeed");
                    btnUpload.IsEnabled = true;
                });
            }
            else
            {
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    MessageBox.Show(errormsg);
                });
            }
        }
    }
}